import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { SimpleAnimate } from '../../../share/animation/my.simple.animation';

import { HttpService } from '../../../share/http/http.service';
import { NoticeService } from '../../../share/service/notice.service';
import { MenuService } from '../../../core/main/service/menu.service';

import { Menu } from '../../../system/model/menu.model';
import { Role } from '../../../system/model/role.model';

import { TreeviewConfig, TreeviewItem, TreeItem } from 'ngx-treeview';

@Component({
  selector: 'hk-role-edit',
  templateUrl: './role.edit.component.html',
  styleUrls: ['./role.edit.component.css'],
  animations: [SimpleAnimate]
})
export class RoleEditComponent implements OnInit {

  id: number;

  role: Role = new Role();

  // treeview的配置
  config = TreeviewConfig.create({
    hasAllCheckBox: false,
    hasFilter: false,
    hasCollapseExpand: true,
    decoupleChildFromParent: false,
    maxHeight: 400
  });

  items: TreeviewItem[] = [];

  menuList = this.menuService.menuData;

  constructor(
    private httpService: HttpService,
    private noticeService: NoticeService,
    private menuService: MenuService,
    private route: ActivatedRoute,
    private router: Router) { }


  ngOnInit() {

    // F5刷新
    let subscription = this.menuService.menuObservable.subscribe(status => {
      this.menuList = this.menuService.menuData;
      if (this.items.length === 0) {
        this.items = this.getMenuItems(this.role.menuIds);
      }
      subscription.unsubscribe();
    });

    if (this.menuList) {
      this.items = this.getMenuItems([]);
    }

    this.route.params.subscribe((params: Params) => {
      this.id = parseInt(params['id'], 10);
      if (this.id) {
        this.getById();
      }
    });
  }

  back() {
    this.router.navigate(['/main/role/list']);
  }

  save() {
    let body = this.role;
    this.httpService.httpPostSuccess('role/save', body, response => {
      this.noticeService.success(response.data);
      this.back();
    });
  }

  onSelectedChange(event) {
    this.role.menuIds = event;
  }

  private getById() {
    this.httpService.httpGetSuccess('role/query/' + this.id, response => {
      this.role.setDataValue(response.data);
      if (this.menuList) {
        this.items = this.getMenuItems(response.data.menuIds);
      }
    });
  }

  /**
   * 获取菜单树
   */
  private getMenuItems(menuIds: any[]): TreeviewItem[] {
    let treeviewItem: TreeviewItem[] = [];
    this.menuList.forEach(e => {
      let treeItem: TreeItem = this.getTreeItem(e, menuIds);
      let menuItem = new TreeviewItem(treeItem);
      treeviewItem.push(menuItem);
    });
    return treeviewItem;
  }

  /**
   * 生成菜单树对象
   */
  private getTreeItem(menu: Menu, menuIds: any[]): any {
    let menuItem: any = {
      text: menu.name,
      value: menu.id,
      checked: false,
    };
    this.checkItem(menuItem, menuIds);
    if (menu.children && menu.children.length > 0) {
      this.addChildrenTreeItem(menuItem, menu.children, menuIds);
    }
    return menuItem;
  }

  /**
   * 添加孩子节点
   */
  private addChildrenTreeItem(menuItem: any, children: Menu[], menuIds: any[]) {
    menuItem.children = [];
    children.forEach(e => {
      let childMenuItem: any = {
        text: e.name,
        value: e.id,
        checked: false,
      };
      this.checkItem(childMenuItem, menuIds);
      menuItem.children.push(childMenuItem);
      if (e.children && e.children.length > 0) {
        this.addChildrenTreeItem(childMenuItem, e.children, menuIds);
      }
    });
  }

  /**
   * 选中
   */
  private checkItem(menuItem: any, menuIds: any[]) {
    menuIds.forEach(menuId => {
      if (menuId === menuItem.value) {
        menuItem.checked = true;
      }
    });
  }

}
